//
//  WLSaveAblumToLocal.m
//  WanLian
//
//  Created by 123456 on 2019/1/16.
//  Copyright © 2019 wanlian. All rights reserved.
//

#import "WLSaveAblumToLocal.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>

@implementation WLSaveAblumToLocal
- (void)writeVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL completionBlock:(void (^)(NSURL *assetURL, NSError *error, NSString *localIdentifier))completionBlock{
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:videoPathURL completionBlock:^(NSURL *assetURL, NSError *error) {
        NSString *local = nil;
        if (assetURL) {
            local = [[PHAsset fetchAssetsWithALAssetURLs:@[assetURL] options:nil] firstObject].localIdentifier;
        }
        completionBlock(assetURL, error, local);
    }];
}

- (void)writeImageDataToSavedPhotosAlbum:(NSData *)imageData metadata:(NSDictionary *)metadata completionBlock:(void (^)(NSURL *assetURL, NSError *error, NSString *localIdentifier))completionBlock {
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeImageDataToSavedPhotosAlbum:imageData metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) {
        NSString *local = nil;
        if (assetURL) {
           local = [[PHAsset fetchAssetsWithALAssetURLs:@[assetURL] options:nil] firstObject].localIdentifier;
        }
        completionBlock(assetURL, error, local);
    }];
}
- (void)writeImageDataToSavedPhotosAlbumWithURL:(NSURL *)imageURL metadata:(NSDictionary *)metadata completionBlock:(void (^)(NSURL *assetURL, NSError *error, NSString *localIdentifier))completionBlock {
    NSData *imageData = [NSData dataWithContentsOfFile:imageURL.path];
    if (imageData.length > 0) {
        [self writeImageDataToSavedPhotosAlbum:imageData metadata:[NSDictionary dictionary] completionBlock:completionBlock];
    }
}
@end
